import * as types from './types'

export const state = {
  autoSignBind: {
    form: {
      mobile: '',
      validity: ''
    }
  }
}

export const getters = {
  autoSignBind_form: state => state.autoSignBind.form,
}

export const actions = {
  signBindSubmit({
    commit
  }, payload) {
    commit(types.SIGN_BIND_SUBMIT, payload);
  }
}

export const mutations = {
  /**
   * 自动还款签约提交
   * 
   * @param {any} state 
   * @param {any} {
   *     vm
   *   } 
   */
  [types.SIGN_BIND_SUBMIT](state, {
    vm
  }) {
    vm.$ajax.post('/sign/showOpenAutoSignUser', vm.$postParams(state.autoSignBind.form))
      .then(res => {
        let d = res.data;
        if (d && d.status == vm.$appConfig.success) {
          let {
            user_id,
            user_name,
            mobile,
            ips_acct_no
          } = d.data.info;
          let validity = d.data.validity;
          vm.$confirm(`是否要为用户${user_name},手机号${mobile}(ips账号:${ips_acct_no})签约自动还款业务,天数:${validity}天`, '签约提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'info'
            })
            .then(() => {
              open(`${vm.$appConfig.domain}/sign/openAutoSign?mobile=${mobile}&user_id=${user_id}&ips_acct_no=${ips_acct_no}&validity=${validity}`);
            })
            .catch(() => {});
        } else {
          vm.$alert(d.msg, '签约信息查询', {});
        }
      })
      .catch(err => {
        vm.$notify.error({
          title: '错误',
          message: '发生了一个错误'
        });
        console.error(err);
      });
  }
}